jQuery(document).ready(function ($) {
    // $(".wysiwyg").wysiwyg();
    CKEDITOR.replaceAll();
    $('a[rel=tooltip]').tooltip();
    $("#checkall_deleteid").click(function () {
        var status = this.checked;
        $("input[name='delete_id']").each(function () {
            this.checked = status;
        })
    });
    $('.popup_tooltip').popover(
        {
            delay:{ show:500, hide:100 }
        }
    );
    $('.form-actions button[type=submit]').click(function(){
        $('html, body').animate({scrollTop:0},500);
    })
});

dashboard.admin = {
    action:{
        add:function (id, path, method, data, callback) {

            jQuery.ajax({
                type:method,
                url:path,
                data:data,
                dataType:'text',
                success:function (result) {
                    dashboard.show_success_fix("#alert", result);
                    $("#" + id)[0].reset();
                }
            });
            return false;
        },
        edit:function (id, path, method, data, callback) {

            jQuery.ajax({
                type:method,
                url:path,
                data:data,
                dataType:'text',
                success:function (result) {
                    dashboard.show_success_fix("#alert", result);
                }
            });
            return false;
        },
        deleteAll:function (table) {
            var listid = "";
            $("input[name='delete_id']").each(function () {
                if (this.checked) listid = listid + "," + this.value;
            })
            listid = listid.substr(1);
            if (listid == "") {
                alert("Bạn chưa chọn record nào !");
                return;
            }
            hoi = confirm("Xóa tất cả record đang chọn ?");
            if (hoi == true) {
                data = 'table=' + table + '&list_id=' + listid;
                jQuery.ajax({
                    type:'POST',
                    url:'ajax/delete_list_record.php',
                    data:data,
                    dataType:'text',
                    success:function (result) {
                        location.reload();
                    }
                });
            }
            return false;
        }
    },
    delete_record:function (table, id) {
        var dataString = "table=" + table + "&id=" + id;
        var path = 'ajax/delete_record.php';
        jQuery.ajax({
            type:'GET',
            url:path,
            data:dataString,
            dataType:'text',
            success:function (result) {
                if (result == "success")
                    window.location.reload();
                else
                    alert("Server had some errors!");
            }
        });
    }
};
dashboard.admin.profile = {
    action:{
        update:function () {
            var dataString = $("#profile-form").serialize();
            var path = 'ajax/user/update.php';
            jQuery.ajax({
                type:'POST',
                url:path,
                data:dataString,
                dataType:'text',
                success:function (result) {
                    dashboard.show_success("#alert", result);
                }
            });
            return false;
        },
        edit:function () {
            var dataString = $("#profile-edit-form").serialize();
            var path = 'ajax/user/edit.php';
            jQuery.ajax({
                type:'POST',
                url:path,
                data:dataString,
                dataType:'text',
                success:function (result) {
                    dashboard.show_success("#alert", result);
                }
            });
            return false;
        },
        add:function (form) {
            var dataString = $(form).serialize();
            var path = 'ajax/user/add.php';
            jQuery.ajax({
                type:'POST',
                url:path,
                data:dataString,
                dataType:'text',
                success:function (result) {
                    dashboard.show_success("#alert", result);
                    $("#profile-add-form")[0].reset();
                }
            });
            return false;
        }
    }
}
